<template>
  <div class="panel-tab__content">
    <el-form size="mini" label-width="90px" @submit.native.prevent>
      <el-form-item label="回路特性">
        <el-select v-model="loopCharacteristics" style="width:100%" @change="changeLoopCharacteristicsType">
          <!--bpmn:MultiInstanceLoopCharacteristics-->
          <el-option label="并行多重事件" value="ParallelMultiInstance" />
          <el-option label="时序多重事件" value="SequentialMultiInstance" />
          <!--bpmn:StandardLoopCharacteristics-->
          <el-option label="循环事件" value="StandardLoop" />
          <el-option label="无" value="Null" />
        </el-select>
      </el-form-item>
      <template v-if="loopCharacteristics === 'ParallelMultiInstance' || loopCharacteristics === 'SequentialMultiInstance'">
        <el-form-item key="loopCardinality" label="循环数量">
          <el-input v-model="loopInstanceForm.loopCardinality" clearable placeholder="请填写循环数量" @change="updateLoopCardinality" />
        </el-form-item>
        <el-form-item key="collection" label="循环集合">
          <el-input v-model="loopInstanceForm.collection" clearable placeholder="请填写循环集合" @change="updateLoopBase" />
        </el-form-item>
        <el-form-item key="elementVariable" label="元素变量">
          <el-input v-model="loopInstanceForm.elementVariable" placeholder="请填写元素变量" clearable @change="updateLoopBase" />
        </el-form-item>
        <el-form-item key="completionCondition" label="完成条件">
          <el-input v-model="loopInstanceForm.completionCondition" placeholder="请填写完成条件" clearable @change="updateLoopCondition" />
        </el-form-item>
        <el-form-item key="async" label="异步状态">
          <el-checkbox v-model="loopInstanceForm.asyncBefore" label="异步前" @change="updateLoopAsync('asyncBefore')" />
          <el-checkbox v-model="loopInstanceForm.asyncAfter" label="异步后" @change="updateLoopAsync('asyncAfter')" />
          <el-checkbox
            v-if="loopInstanceForm.asyncAfter || loopInstanceForm.asyncBefore"
            v-model="loopInstanceForm.exclusive"
            label="排除"
            @change="updateLoopAsync('exclusive')" />
        </el-form-item>
        <el-form-item v-if="loopInstanceForm.asyncAfter || loopInstanceForm.asyncBefore" key="timeCycle" label="重试周期" prop="timeCycle">
          <el-input v-model="loopInstanceForm.timeCycle" clearable placeholder="请填写重试周期" @change="updateLoopTimeCycle" />
        </el-form-item>
      </template>
    </el-form>
    <el-tooltip
      class="item"
      effect="dark"
      placement="top"
      popper-class="shiyongshuoming-tooltip">
      <div slot="content" style="line-height: 25px;font-size: 13px;">
        1.'${flowUtil.stringToList(assigneeUserIdList)}'，将字符串转换成集合，暴露的SpringBean方法<br>
        2.多实例任务Activiti默认会创建3个流程变量，nrOfInstances:实例总数，nrOfActiveInstances:当前活跃的，也就是当前还未完成的，对于顺序的多实例，此值总是1,nrOfCompletedInstances:已完成的实例个数。<br>
        3.状态:不启动多实例,则只会创建一个任务，默认不启动，不启动多实例，一下配置都无效，true:顺序执行，fasle:并行,同时执行。<br>
        4.循环数量:指定创建多任务的数量。可使用表达式从流程变量获取。<br>
        5.循环集合:流程变量中一个集合类型变量的变量名称。根据集合元素多少创建任务数量。可使用表达式。例:流程变量：assigneeUserIdList=[user1,user2]，可用assigneeUserIdList。<br>
        6.集合元素:集合中每个元素的变量名称，可在每个任务中获取,可使用表达式。例：集合为当定义集合元素名称为:assigneeUserId,可在任务直接指派人员用表达式${assigneeUserId}获取，用于动态会签。<br>
        7.结束条件:多实例活动结束的条件，默认为完成多全部实例，当表达式返回true时结束多实例活动。例：${nrOfCompletedInstances/nrOfInstances>=0.6} 说明当有60%的任务完成时，会完成此多实例，删除其他未完成的，继续下面的流程。<br>
      </div>
      <span style="color: #e6a23c;">使用说明</span>
    </el-tooltip>
  </div>
</template>

<script>
export default {
  name: 'ElementMultiInstance',
  props: {
    businessObject: {
      type: Object,
      default: () => { return null }
    },
    type: {
      type: String,
      default: null
    }
  },
  inject: {
    prefix: 'prefix'
  },
  data() {
    return {
      loopCharacteristics: '',
      // 默认配置，用来覆盖原始不存在的选项，避免报错
      defaultLoopInstanceForm: {
        completionCondition: '',
        loopCardinality: '',
        extensionElements: [],
        asyncAfter: false,
        asyncBefore: false,
        exclusive: false
      },
      loopInstanceForm: {}
    }
  },
  watch: {
    businessObject: {
      immediate: true,
      handler(val) {
        this.bpmnElement = window.bpmnInstances.bpmnElement
        this.getElementLoop(val)
      }
    }
  },
  beforeDestroy() {
    this.multiLoopInstance = null
    this.bpmnElement = null
  },

  methods: {
    getElementLoop(businessObject) {
      if (!businessObject.loopCharacteristics) {
        this.loopCharacteristics = 'Null'
        this.loopInstanceForm = {}
        return
      }
      if (businessObject.loopCharacteristics.$type === 'bpmn:StandardLoopCharacteristics') {
        this.loopCharacteristics = 'StandardLoop'
        this.loopInstanceForm = {}
        return
      }
      if (businessObject.loopCharacteristics.isSequential) {
        this.loopCharacteristics = 'SequentialMultiInstance'
      } else {
        this.loopCharacteristics = 'ParallelMultiInstance'
      }
      // 合并配置
      this.loopInstanceForm = {
        ...this.defaultLoopInstanceForm,
        ...businessObject.loopCharacteristics,
        completionCondition: businessObject.loopCharacteristics?.completionCondition?.body ?? '',
        loopCardinality: businessObject.loopCharacteristics?.loopCardinality?.body ?? ''
      }
      // 保留当前元素 businessObject 上的 loopCharacteristics 实例
      this.multiLoopInstance = window.bpmnInstances.bpmnElement.businessObject.loopCharacteristics
      // 更新表单
      if (
        businessObject.loopCharacteristics.extensionElements &&
        businessObject.loopCharacteristics.extensionElements.values &&
        businessObject.loopCharacteristics.extensionElements.values.length
      ) {
        this.$set(this.loopInstanceForm, 'timeCycle', businessObject.loopCharacteristics.extensionElements.values[0].body)
      }
    },
    changeLoopCharacteristicsType(type) {
      // this.loopInstanceForm = { ...this.defaultLoopInstanceForm }; // 切换类型取消原表单配置
      // 取消多实例配置
      if (type === 'Null') {
        window.bpmnInstances.modeling.updateProperties(this.bpmnElement, { loopCharacteristics: null })
        return
      }
      // 配置循环
      if (type === 'StandardLoop') {
        const loopCharacteristicsObject = window.bpmnInstances.moddle.create('bpmn:StandardLoopCharacteristics')
        window.bpmnInstances.modeling.updateProperties(this.bpmnElement, {
          loopCharacteristics: loopCharacteristicsObject
        })
        this.multiLoopInstance = null
        return
      }
      // 时序
      if (type === 'SequentialMultiInstance') {
        this.multiLoopInstance = window.bpmnInstances.moddle.create('bpmn:MultiInstanceLoopCharacteristics', { isSequential: true })
      } else {
        this.multiLoopInstance = window.bpmnInstances.moddle.create('bpmn:MultiInstanceLoopCharacteristics')
      }
      window.bpmnInstances.modeling.updateProperties(this.bpmnElement, {
        loopCharacteristics: this.multiLoopInstance
      })
    },
    // 循环基数
    updateLoopCardinality(cardinality) {
      let loopCardinality = null
      if (cardinality && cardinality.length) {
        loopCardinality = window.bpmnInstances.moddle.create('bpmn:FormalExpression', { body: cardinality })
      }
      window.bpmnInstances.modeling.updateModdleProperties(this.bpmnElement, this.multiLoopInstance, { loopCardinality })
    },
    // 完成条件
    updateLoopCondition(condition) {
      let completionCondition = null
      if (condition && condition.length) {
        completionCondition = window.bpmnInstances.moddle.create('bpmn:FormalExpression', { body: condition })
      }
      window.bpmnInstances.modeling.updateModdleProperties(this.bpmnElement, this.multiLoopInstance, { completionCondition })
    },
    // 重试周期
    updateLoopTimeCycle(timeCycle) {
      const extensionElements = window.bpmnInstances.moddle.create('bpmn:ExtensionElements', {
        values: [
          window.bpmnInstances.moddle.create(`${this.prefix}:FailedJobRetryTimeCycle`, {
            body: timeCycle
          })
        ]
      })
      window.bpmnInstances.modeling.updateModdleProperties(this.bpmnElement, this.multiLoopInstance, { extensionElements })
    },
    // 直接更新的基础信息
    updateLoopBase() {
      window.bpmnInstances.modeling.updateModdleProperties(this.bpmnElement, this.multiLoopInstance, {
        collection: this.loopInstanceForm.collection || null,
        elementVariable: this.loopInstanceForm.elementVariable || null
      })
    },
    // 各异步状态
    updateLoopAsync(key) {
      const { asyncBefore, asyncAfter } = this.loopInstanceForm
      let asyncAttr = Object.create(null)
      if (!asyncBefore && !asyncAfter) {
        this.$set(this.loopInstanceForm, 'exclusive', false)
        asyncAttr = { asyncBefore: false, asyncAfter: false, exclusive: false, extensionElements: null }
      } else {
        asyncAttr[key] = this.loopInstanceForm[key]
      }
      window.bpmnInstances.modeling.updateModdleProperties(this.bpmnElement, this.multiLoopInstance, asyncAttr)
    }
  }
}
</script>
<style lang="scss">
.shiyongshuoming-tooltip{
  width: 300px;
}
</style>
